home *** CD-ROM | disk | FTP | other *** search
/ Cricao de Sites - 650 Layouts Prontos / WebMasters.iso / Editores / nvu-1.0-win32-installer-full.exe / {app} / components / nsProxyAutoConfig.js < prev    next >
Text File  |  2004-09-02  |  15KB  |  441 lines

  1. /* -*- Mode: Java; tab-width: 4; c-basic-offset: 4; -*- */
  2. /* ***** BEGIN LICENSE BLOCK *****
  3.  * Version: NPL 1.1/GPL 2.0/LGPL 2.1
  4.  *
  5.  * The contents of this file are subject to the Netscape Public License
  6.  * Version 1.1 (the "License"); you may not use this file except in
  7.  * compliance with the License. You may obtain a copy of the License at
  8.  * http://www.mozilla.org/NPL/
  9.  *
  10.  * Software distributed under the License is distributed on an "AS IS" basis,
  11.  * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
  12.  * for the specific language governing rights and limitations under the
  13.  * License.
  14.  *
  15.  * The Original Code is mozilla.org code.
  16.  *
  17.  * The Initial Developer of the Original Code is 
  18.  * Netscape Communications Corporation.
  19.  * Portions created by the Initial Developer are Copyright (C) 1998
  20.  * the Initial Developer. All Rights Reserved.
  21.  *
  22.  * Contributor(s):
  23.  *   Akhil Arora <akhil.arora@sun.com>
  24.  *   Tomi Leppikangas <Tomi.Leppikangas@oulu.fi>
  25.  *
  26.  * Alternatively, the contents of this file may be used under the terms of
  27.  * either the GNU General Public License Version 2 or later (the "GPL"), or 
  28.  * the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
  29.  * in which case the provisions of the GPL or the LGPL are applicable instead
  30.  * of those above. If you wish to allow use of your version of this file only
  31.  * under the terms of either the GPL or the LGPL, and not to allow others to
  32.  * use your version of this file under the terms of the NPL, indicate your
  33.  * decision by deleting the provisions above and replace them with the notice
  34.  * and other provisions required by the GPL or the LGPL. If you do not delete
  35.  * the provisions above, a recipient may use your version of this file under
  36.  * the terms of any one of the NPL, the GPL or the LGPL.
  37.  *
  38.  * ***** END LICENSE BLOCK ***** */
  39.  
  40. /*
  41.    Script for Proxy Auto Config in the new world order.
  42.        - Gagan Saksena 04/24/00 
  43. */
  44.  
  45. const kPAC_CONTRACTID = "@mozilla.org/network/proxy-auto-config;1";
  46. const kIOSERVICE_CONTRACTID = "@mozilla.org/network/io-service;1";
  47. const kDNS_CONTRACTID = "@mozilla.org/network/dns-service;1";
  48. const kPAC_CID = Components.ID("{63ac8c66-1dd2-11b2-b070-84d00d3eaece}");
  49. const nsIProxyAutoConfig = Components.interfaces.nsIProxyAutoConfig;
  50. const nsIIOService = Components.interfaces['nsIIOService'];
  51. const nsIDNSService = Components.interfaces.nsIDNSService;
  52. const nsIRequest = Components.interfaces.nsIRequest;
  53.  
  54. // implementor of nsIProxyAutoConfig
  55. function nsProxyAutoConfig() {};
  56.  
  57. // global variable that will hold the downloaded js 
  58. var pac = null;
  59. //hold PAC's URL, used in evalAsCodebase()
  60. var pacURL;
  61. // ptr to eval'ed FindProxyForURL function
  62. var LocalFindProxyForURL = null;
  63. // sendbox in which we eval loaded autoconfig js file
  64. var ProxySandBox = null;
  65.  
  66. nsProxyAutoConfig.prototype = {
  67.     sis: null,
  68.     done: false,
  69.  
  70.     getProxyForURI: function(uri) {
  71.         // If we're not done loading the pac yet, wait (ideally). For
  72.         // now, just return DIRECT to avoid loops. A simple mutex
  73.         // between getProxyForURI and loadPACFromURI locks-up the
  74.         // browser.
  75.         if (!this.done)
  76.             return null;
  77.  
  78.         if (!LocalFindProxyForURL)
  79.             return null;
  80.  
  81.         // Call the original function-
  82.         return LocalFindProxyForURL(uri.spec, uri.host);
  83.     },
  84.  
  85.     loadPACFromURI: function(uri, ioService) {
  86.         this.done = false;
  87.         var channel = ioService.newChannelFromURI(uri);
  88.         // don't cache the PAC content
  89.         channel.loadFlags |= nsIRequest.LOAD_BYPASS_CACHE;
  90.         pacURL = uri.spec;
  91.         channel.notificationCallbacks = this;
  92.         channel.asyncOpen(this, null);
  93.         Components.returnCode = Components.results.NS_OK;
  94.     },
  95.  
  96.     // nsIInterfaceRequestor interface
  97.     getInterface: function(iid, instance) {
  98.         if (iid.equals(Components.interfaces.nsIAuthPrompt)) {
  99.             // use the window watcher service to get a nsIAuthPrompt impl
  100.             var ww = Components.classes["@mozilla.org/embedcomp/window-watcher;1"]
  101.                                .getService(Components.interfaces.nsIWindowWatcher);
  102.             return ww.getNewAuthPrompter(null);
  103.         }
  104.         Components.returnCode = Components.results.NS_ERROR_NO_INTERFACE;
  105.         return null;
  106.     },
  107.  
  108.     // nsIStreamListener interface
  109.     onStartRequest: function(request, ctxt) { 
  110.         pac = '';
  111.         LocalFindProxyForURL=null;
  112.         this.sis = 
  113.         Components.Constructor('@mozilla.org/scriptableinputstream;1',
  114.                                'nsIScriptableInputStream', 
  115.                                'init');
  116.     },
  117.  
  118.     onStopRequest: function(request, ctxt, status) {
  119.         if(!ProxySandBox) {
  120.            ProxySandBox = new Sandbox();
  121.         }
  122.         // add predefined functions to pac
  123.         var mypac = pacUtils + pac;
  124.         ProxySandBox.myIpAddress = myIpAddress;
  125.         ProxySandBox.dnsResolve = dnsResolve;
  126.         ProxySandBox.alert = proxyAlert;
  127.         // evaluate loaded js file
  128.         evalInSandbox(mypac, ProxySandBox, pacURL);
  129.         LocalFindProxyForURL=ProxySandBox.FindProxyForURL;
  130.         this.done = true;
  131.     },
  132.  
  133.     onDataAvailable: function(request, ctxt, inStream, sourceOffset, count) {
  134.         var ins = new this.sis(inStream);
  135.         pac += ins.read(count);
  136.     }
  137. }
  138.  
  139. function proxyAlert(msg) {
  140.     try {
  141.         var cns = Components.classes["@mozilla.org/consoleservice;1"]
  142.                             .getService(Components.interfaces.nsIConsoleService);
  143.         cns.logStringMessage("PAC-alert: "+msg);
  144.     } catch (e) {
  145.         dump("PAC: proxyAlert ERROR: "+e+"\n");
  146.     }
  147. }
  148.  
  149. // wrapper for getting local IP address called by PAC file
  150. function myIpAddress() {
  151.     try {
  152.         return dns.resolve(dns.myHostName, 0).getNextAddrAsString();
  153.     } catch (e) {
  154.         return '127.0.0.1';
  155.     }
  156. }
  157.  
  158. // wrapper for resolving hostnames called by PAC file
  159. function dnsResolve(host) {
  160.     try {
  161.         return dns.resolve(host, 0).getNextAddrAsString();
  162.     } catch (e) {
  163.         return null;
  164.     }
  165. }
  166.  
  167. var pacModule = new Object();
  168.  
  169. pacModule.registerSelf =
  170.     function (compMgr, fileSpec, location, type) {
  171.         compMgr = compMgr.QueryInterface(Components.interfaces.nsIComponentRegistrar);
  172.         compMgr.registerFactoryLocation(kPAC_CID,
  173.                                         "nsProxyAutoConfig",
  174.                                         kPAC_CONTRACTID,
  175.                                         fileSpec, 
  176.                                         location, 
  177.                                         type);
  178.     }
  179.  
  180. pacModule.getClassObject =
  181. function (compMgr, cid, iid) {
  182.         if (!cid.equals(kPAC_CID))
  183.             throw Components.results.NS_ERROR_NO_INTERFACE;
  184.  
  185.         if (!iid.equals(Components.interfaces.nsIFactory))
  186.             throw Components.results.NS_ERROR_NOT_IMPLEMENTED;
  187.  
  188.         return pacFactory;
  189.     }
  190.  
  191. pacModule.canUnload =
  192.     function (compMgr) {
  193.         return true;
  194.     }
  195.  
  196. var pacFactory = new Object();
  197. pacFactory.createInstance =
  198.     function (outer, iid) {
  199.         if (outer != null)
  200.             throw Components.results.NS_ERROR_NO_AGGREGATION;
  201.  
  202.         if (!iid.equals(nsIProxyAutoConfig) &&
  203.             !iid.equals(Components.interfaces.nsIStreamListener) &&
  204.             !iid.equals(Components.interfaces.nsIRequestObserver) &&
  205.             !iid.equals(Components.interfaces.nsISupports)) {
  206.             // shouldn't this be NO_INTERFACE?
  207.             throw Components.results.NS_ERROR_INVALID_ARG;
  208.         }
  209.         return PacMan;
  210.     }
  211.  
  212. function NSGetModule(compMgr, fileSpec) {
  213.     return pacModule;
  214. }
  215.  
  216. var PacMan = new nsProxyAutoConfig() ;
  217.  
  218. var ios = Components.classes[kIOSERVICE_CONTRACTID].getService(nsIIOService);
  219. var dns = Components.classes[kDNS_CONTRACTID].getService(nsIDNSService);
  220.  
  221. var pacUtils = 
  222. "function dnsDomainIs(host, domain) {\n" +
  223. "    return (host.length >= domain.length &&\n" +
  224. "            host.substring(host.length - domain.length) == domain);\n" +
  225. "}\n" +
  226.  
  227. "function dnsDomainLevels(host) {\n" +
  228. "    return host.split('.').length-1;\n" +
  229. "}\n" +
  230.  
  231. "function convert_addr(ipchars) {\n"+
  232. "    var bytes = ipchars.split('.');\n"+
  233. "    var result = ((bytes[0] & 0xff) << 24) |\n"+
  234. "                 ((bytes[1] & 0xff) << 16) |\n"+
  235. "                 ((bytes[2] & 0xff) <<  8) |\n"+
  236. "                  (bytes[3] & 0xff);\n"+
  237. "    return result;\n"+
  238. "}\n"+
  239.  
  240. "function isInNet(ipaddr, pattern, maskstr) {\n"+
  241. "    var test = /^(\\d{1,4})\\.(\\d{1,4})\\.(\\d{1,4})\\.(\\d{1,4})$/(ipaddr);\n"+
  242. "    if (test == null) {\n"+
  243. "        ipaddr = dnsResolve(ipaddr);\n"+
  244. "        if (ipaddr == null)\n"+
  245. "            return false;\n"+
  246. "    } else if (test[1] > 255 || test[2] > 255 || \n"+
  247. "               test[3] > 255 || test[4] > 255) {\n"+
  248. "        return false;    // not an IP address\n"+
  249. "    }\n"+
  250. "    var host = convert_addr(ipaddr);\n"+
  251. "    var pat  = convert_addr(pattern);\n"+
  252. "    var mask = convert_addr(maskstr);\n"+
  253. "    return ((host & mask) == (pat & mask));\n"+
  254. "    \n"+
  255. "}\n"+
  256.  
  257. "function isPlainHostName(host) {\n" +
  258. "    return (host.search('\\\\.') == -1);\n" +
  259. "}\n" +
  260.  
  261. "function isResolvable(host) {\n" +
  262. "    var ip = dnsResolve(host);\n" +
  263. "    return (ip != null);\n" +
  264. "}\n" +
  265.  
  266. "function localHostOrDomainIs(host, hostdom) {\n" +
  267. "    if (isPlainHostName(host)) {\n" +
  268. "        return (hostdom.search('/^' + host + '/') != -1);\n" +
  269. "    }\n" +
  270. "    else {\n" +
  271. "        return (host == hostdom); //TODO check \n" +
  272. "    }\n" +
  273. "}\n" +
  274.  
  275. "function shExpMatch(url, pattern) {\n" +
  276. "   pattern = pattern.replace(/\\./g, '\\\\.');\n" +
  277. "   pattern = pattern.replace(/\\*/g, '.*');\n" +
  278. "   pattern = pattern.replace(/\\?/g, '.');\n" +
  279. "   var newRe = new RegExp('^'+pattern+'$');\n" +
  280. "   return newRe.test(url);\n" +
  281. "}\n" +
  282.  
  283. "var wdays = new Array('SUN', 'MON', 'TUE', 'WED', 'THU', 'FRI', 'SAT');\n" +
  284.  
  285. "var monthes = new Array('JAN', 'FEB', 'MAR', 'APR', 'MAY', 'JUN', 'JUL', 'AUG', 'SEP', 'OCT', 'NOV', 'DEC');\n"+
  286.  
  287. "function weekdayRange() {\n" +
  288. "    function getDay(weekday) {\n" +
  289. "        for (var i = 0; i < 6; i++) {\n" +
  290. "            if (weekday == wdays[i]) \n" +
  291. "                return i;\n" +
  292. "        }\n" +
  293. "        return -1;\n" +
  294. "    }\n" +
  295. "    var date = new Date();\n" +
  296. "    var argc = arguments.length;\n" +
  297. "    var wday;\n" +
  298. "    if (argc < 1)\n" +
  299. "        return false;\n" +
  300. "    if (arguments[argc - 1] == 'GMT') {\n" +
  301. "        argc--;\n" +
  302. "        wday = date.getUTCDay();\n" +
  303. "    } else {\n" +
  304. "        wday = date.getDay();\n" +
  305. "    }\n" +
  306. "    var wd1 = getDay(arguments[0]);\n" +
  307. "    var wd2 = (argc == 2) ? getDay(arguments[1]) : wd1;\n" +
  308. "    return (wd1 == -1 || wd2 == -1) ? false\n" +
  309. "                                    : (wd1 <= wday && wday <= wd2);\n" +
  310. "}\n" +
  311.  
  312. "function dateRange() {\n" +
  313. "    function getMonth(name) {\n" +
  314. "        for (var i = 0; i < 6; i++) {\n" +
  315. "            if (name == monthes[i])\n" +
  316. "                return i;\n" +
  317. "        }\n" +
  318. "        return -1;\n" +
  319. "    }\n" +
  320. "    var date = new Date();\n" +
  321. "    var argc = arguments.length;\n" +
  322. "    if (argc < 1) {\n" +
  323. "        return false;\n" +
  324. "    }\n" +
  325. "    var isGMT = (arguments[argc - 1] == 'GMT');\n" +
  326. "\n" +
  327. "    if (isGMT) {\n" +
  328. "        argc--;\n" +
  329. "    }\n" +
  330. "    // function will work even without explict handling of this case\n" +
  331. "    if (argc == 1) {\n" +
  332. "        var tmp = parseInt(arguments[0]);\n" +
  333. "        if (isNaN(tmp)) {\n" +
  334. "            return ((isGMT ? date.getUTCMonth() : date.getMonth()) ==\n" +
  335. "getMonth(arguments[0]));\n" +
  336. "        } else if (tmp < 32) {\n" +
  337. "            return ((isGMT ? date.getUTCDate() : date.getDate()) == tmp);\n" +
  338. "        } else { \n" +
  339. "            return ((isGMT ? date.getUTCFullYear() : date.getFullYear()) ==\n" +
  340. "tmp);\n" +
  341. "        }\n" +
  342. "    }\n" +
  343. "    var year = date.getFullYear();\n" +
  344. "    var date1, date2;\n" +
  345. "    date1 = new Date(year,  0,  1,  0,  0,  0);\n" +
  346. "    date2 = new Date(year, 11, 31, 23, 59, 59);\n" +
  347. "    var adjustMonth = false;\n" +
  348. "    for (var i = 0; i < (argc >> 1); i++) {\n" +
  349. "        var tmp = parseInt(arguments[i]);\n" +
  350. "        if (isNaN(tmp)) {\n" +
  351. "            var mon = getMonth(arguments[i]);\n" +
  352. "            date1.setMonth(mon);\n" +
  353. "        } else if (tmp < 32) {\n" +
  354. "            adjustMonth = (argc <= 2);\n" +
  355. "            date1.setDate(tmp);\n" +
  356. "        } else {\n" +
  357. "            date1.setFullYear(tmp);\n" +
  358. "        }\n" +
  359. "    }\n" +
  360. "    for (var i = (argc >> 1); i < argc; i++) {\n" +
  361. "        var tmp = parseInt(arguments[i]);\n" +
  362. "        if (isNaN(tmp)) {\n" +
  363. "            var mon = getMonth(arguments[i]);\n" +
  364. "            date2.setMonth(mon);\n" +
  365. "        } else if (tmp < 32) {\n" +
  366. "            date2.setDate(tmp);\n" +
  367. "        } else {\n" +
  368. "            date2.setFullYear(tmp);\n" +
  369. "        }\n" +
  370. "    }\n" +
  371. "    if (adjustMonth) {\n" +
  372. "        date1.setMonth(date.getMonth());\n" +
  373. "        date2.setMonth(date.getMonth());\n" +
  374. "    }\n" +
  375. "    if (isGMT) {\n" +
  376. "    var tmp = date;\n" +
  377. "        tmp.setFullYear(date.getUTCFullYear());\n" +
  378. "        tmp.setMonth(date.getUTCMonth());\n" +
  379. "        tmp.setDate(date.getUTCDate());\n" +
  380. "        tmp.setHours(date.getUTCHours());\n" +
  381. "        tmp.setMinutes(date.getUTCMinutes());\n" +
  382. "        tmp.setSeconds(date.getUTCSeconds());\n" +
  383. "        date = tmp;\n" +
  384. "    }\n" +
  385. "    return ((date1 <= date) && (date <= date2));\n" +
  386. "}\n" +
  387.  
  388. "function timeRange() {\n" +
  389. "    var argc = arguments.length;\n" +
  390. "    var date = new Date();\n" +
  391. "    var isGMT= false;\n"+
  392. "\n" +
  393. "    if (argc < 1) {\n" +
  394. "        return false;\n" +
  395. "    }\n" +
  396. "    if (arguments[argc - 1] == 'GMT') {\n" +
  397. "        isGMT = true;\n" +
  398. "        argc--;\n" +
  399. "    }\n" +
  400. "\n" +
  401. "    var hour = isGMT ? date.getUTCHours() : date.getHours();\n" +
  402. "    var date1, date2;\n" +
  403. "    date1 = new Date();\n" +
  404. "    date2 = new Date();\n" +
  405. "\n" +
  406. "    if (argc == 1) {\n" +
  407. "        return (hour == arguments[0]);\n" +
  408. "    } else if (argc == 2) {\n" +
  409. "        return ((arguments[0] <= hour) && (hour <= arguments[1]));\n" +
  410. "    } else {\n" +
  411. "        switch (argc) {\n" +
  412. "        case 6:\n" +
  413. "            date1.setSeconds(arguments[2]);\n" +
  414. "            date2.setSeconds(arguments[5]);\n" +
  415. "        case 4:\n" +
  416. "            var middle = argc >> 1;\n" +
  417. "            date1.setHours(arguments[0]);\n" +
  418. "            date1.setMinutes(arguments[1]);\n" +
  419. "            date2.setHours(arguments[middle]);\n" +
  420. "            date2.setMinutes(arguments[middle + 1]);\n" +
  421. "            if (middle == 2) {\n" +
  422. "                date2.setSeconds(59);\n" +
  423. "            }\n" +
  424. "            break;\n" +
  425. "        default:\n" +
  426. "          throw 'timeRange: bad number of arguments'\n" +
  427. "        }\n" +
  428. "    }\n" +
  429. "\n" +
  430. "    if (isGMT) {\n" +
  431. "        date.setFullYear(date.getUTCFullYear());\n" +
  432. "        date.setMonth(date.getUTCMonth());\n" +
  433. "        date.setDate(date.getUTCDate());\n" +
  434. "        date.setHours(date.getUTCHours());\n" +
  435. "        date.setMinutes(date.getUTCMinutes());\n" +
  436. "        date.setSeconds(date.getUTCSeconds());\n" +
  437. "    }\n" +
  438. "    return ((date1 <= date) && (date <= date2));\n" +
  439. "}\n"
  440.  
  441.